// components/generate-pic/generate-pic.js
import activityApi from "../../service/activityApi";
import sweepstakeApi from "../../service/sweepstakeApi";
import giftApi from "../../service/giftApi";
const app = getApp()
const SUCCESS_CODE = app.globalData.codeConfig.errCode.SUCCESS_CODE
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        cardPopup: {
            type: Boolean,
            value: false,
        },
        cardType: {
            type: String,
            value: ''
        }, // 卡券类型
        popupType: {
            type: String,
            value: 'REWARD_NOTICE'
        }, // 获奖类型
        giftType: {
            type: String,
            value: '1'
        }, // 礼物类型 1、优惠券2、商品券
        giftId: {
            type: String,
            value: ''
        },
        giftAmount: {
            type: Number,
            value: null
        },
        luckDrawData: {
            type: Object,
            value: {}
        }, // 每日抽奖奖励卡券
    },
    /**
     * 组件的初始数据 
     */
    data: {
        cardData: {},
        senderInfo: {
            nickName: '',
            headUrl: ''
        }, // 赠送人信息
        giftSendSuccess: false, // 送出的礼物在区块链是否处理完成
        picBaseUrl: app.globalData.picBaseUrl,
        rewardType: '2',
        timeOver: false,
        name: '虫虫',
        actionStatusCode: app.globalData.codeConfig.actionStatus, // 红包状态
        receiveStatus: app.globalData.codeConfig.receiveStatus, // 领取礼物状态
        receiveStatusText: ['领取成功', '你来晚啦，该券已被领取啦~', '已经领完了', '该券已超过领取时间'],
    },
    // 在组件完全初始化完毕、进入页面节点树后
    attached() {},
    // 在组件实例进入页面节点树时执行
    ready() {
        if (!!wx.getStorageSync('hadAuthFlag')) {
            if (this.data.popupType === 'THANKS_GIFT') { // 感恩礼品 
                this.setData({
                    rewardType: app.globalData.codeConfig.popUpRewardType.THANKS_GIFT
                })
                this.rewardCoupon()
            } else if (this.data.popupType === 'GIFTS_FROM_FRIENDS') { // 好友送礼
                this.setData({
                    rewardType: app.globalData.codeConfig.popUpRewardType.GIFTS_FROM_FRIENDS
                })
                this.getGift()
            } else if (this.data.popupType === 'CONGRATULATIONS_GET_REWARD') { // 恭喜获奖
                this.setData({
                    rewardType: app.globalData.codeConfig.popUpRewardType.CONGRATULATIONS_GET_REWARD,
                })
                // this.getRewardNotice() // 投票中奖隐藏
            }
        }
    },


    /**
     * 组件的方法列表
     */
    methods: {
        // 抽奖弹出奖励卡券
        onSetCardData(val) {
            this.setData({
                cardData: val
            })
        },
        // 查询礼物信息
        getGift() {
            let that = this
            if (!this.data.giftSendSuccess) {
                wx.showLoading({
                    title: '加载中'
                })
            }
            giftApi.getGiftInfo({ giftId: this.data.giftId }).then(res => {
                console.log(res, '礼物信息');
                if (res.data.errCode === SUCCESS_CODE) {
                    wx.hideLoading()
                    this.setData({
                        cardData: res.data.data.giftInfo,
                        senderInfo: res.data.data.senderInfo,
                        giftSendSuccess: false
                    })
                    if (res.data.data.giftInfo.actionStatus === this.data.actionStatusCode.GET_SUCCESS) { // 已领取过不显示弹窗
                        this.setData({
                            cardPopup: false
                        })
                    } else {
                        this.setData({
                            cardPopup: true
                        })
                    }
                } else if (res.data.errCode === '600020') {
                    /* 
                     送出红包之后，区块链处理需要一段时间，
                     立即领取会出现异常，所以轮询直到区块链处理完成才显示弹窗
                    */
                    this.setData({
                        giftSendSuccess: true
                    })
                    // 1.5秒后再次查询
                    setTimeout(function() {
                        that.getGift()
                    }, 1500)
                } else {
                    this.setData({
                        giftSendSuccess: false
                    })
                    wx.hideLoading()
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
            })
        },

        // 立即领取
        onNowGet() {
            if (!this.data.cardPopup) return false
            this.setData({
                cardPopup: false
            })
            giftApi.receiveGift({ giftId: this.data.giftId }).then(res => {
                if (res.data.errCode === SUCCESS_CODE) {
                    if (res.data.data.receiveStatus === this.data.receiveStatus.RECEIVE_SUCCESS) {
                        console.log(this.data.cardData.couponType, 'couponType');
                        if (this.data.cardData.couponType !== '5') {
                            wx.showToast({
                                title: '领取成功'
                            })
                            // 优惠券领取时暂时不跳“我的优惠券”页面
                            // app.globalData.receiveCouponStatus = this.data.cardData.overdueFlag // 礼物是否过期
                            // wx.navigateTo({
                            //     url: '/pages/myCoupon/myCoupon?receiveGift=领取成功'
                            // })
                        } else {
                            wx.navigateTo({
                                url: `/pages/giftReceived/giftReceived?giftId=${this.data.giftId}`
                            })
                        }
                    } else {
                        wx.showToast({
                            title: this.data.receiveStatusText[res.data.data.receiveStatus - 0],
                            icon: 'none'
                        })
                    }
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
            })
        },

        // 去券包
        onToCouponBag() {
            this.setData({
                cardPopup: false
            })
            if (this.data.cardData.couponType !== '5') {
                app.globalData.receiveCouponStatus = this.data.cardData.overdueFlag // 礼物是否过期
                wx.navigateTo({
                    url: '/pages/myCoupon/myCoupon'
                })
            } else {
                wx.switchTab({
                    url: '/pages/couponBag/couponBag'
                })
            }
        },
        // 获取奖励卡券
        rewardCoupon() {
            activityApi.rewardCoupon({ rewardType: this.data.rewardType }).then(res => {
                if (res.data.errCode === SUCCESS_CODE) {
                    this.setData({
                        cardData: res.data.data.couponList[0],
                    })
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
            })
        },
        // 关闭获奖弹窗
        closeCardPopup() {
            this.getRewardRead()
            this.setData({
                cardPopup: false
            })
        },
        // 立即使用
        onNowuse() {
            this.getRewardRead()
            this.setData({
                cardPopup: false
            })
            wx.switchTab({
                url: '/pages/index/index'
            })
        },

        // 我的卡券
        onCardTicket() {
            this.getRewardRead()
            wx.redirectTo({
                url: '/pages/myCoupon/myCoupon'
            })
        },

        // 标记已读信息
        getRewardRead() {
            let that = this
            sweepstakeApi.getRewardRead({ winningId: that.data.cardData.winningId })
        },
        // 查询用户是否中奖
        getRewardNotice() {
            let that = this
            sweepstakeApi.getRewardNotice().then(res => {
                if (res.data.errCode === SUCCESS_CODE) {
                    try {
                        let _couponList = res.data.data.couponList
                        let YES = app.globalData.codeConfig.rewardNoticeFlag.YES
                        if ((_couponList.length > 0) && (_couponList[0].noticeFlag === YES)) {
                            // that.triggerEvent('reward', {}, {})
                            that.setData({
                                cardData: _couponList[0],
                                cardPopup: true
                            })
                        } else {
                            that.setData({
                                cardPopup: false
                            })
                        }
                    } catch (err) {
                        console.log('get reward notice error: ', err)
                    }
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
            })
        },
    }
})